Using regular expressions (regex) in sed

您所在的位置:网站首页 regex101 bash sed Using regular expressions (regex) in sed

Using regular expressions (regex) in sed

2023-03-29 20:31| 来源: 网络整理| 查看: 265

\$GLOBALS\['\''timechecks'\''\]=addTimeCheck_sparky[(]$GLOBALS ^

There's an unescaped $ there.

\['\''timechecks'\''\][,][ ]number_format[(]microtime[(]true[)] [,]6[,]'\''\.'\''[,]'\'''\''[)][,][ ]__LINE__[],[ ]basename[(]__FILE__[)][)][;] ^^

And that should probably be [,].

Not escaping that $ doesn't even really matter (at least with GNU sed), but that [],[ ] is bracket expression with [], and space inside. It's a valid regex though, just not what you wanted, so it won't produce any errors.

But really, quoting is so painful to do. Sometimes it's better to just avoid it.

Let's just put the pattern and replacements strings in some files, along with a test file:

$ cat pat $GLOBALS['timechecks']=addTimeCheck_sparky($GLOBALS['timechecks'], number_format(microtime(true),6,'.',''), __LINE__, basename(__FILE__)); $ cat repl hello! $ cat test.txt foo $GLOBALS['timechecks']=addTimeCheck_sparky($GLOBALS['timechecks'], number_format(microtime(true),6,'.',''), __LINE__, basename(__FILE__)); bar

and then, replace the strings with Perl:

$ pat=$(< pat) repl=$(< repl) perl -i.bak -pe 's/\Q$ENV{pat}/$ENV{repl}/' test.txt $ cat test.txt foo hello! bar

When the strings are read from files, there's no need for quoting on the shell command line. Also, when the pattern comes from a variable, and \Q is used, there's no need to escape the special characters in the pattern. Here, I passed the strings to Perl through the environment, since it works better with -i than command line arguments. -p makes perl act a bit like sed in that it runs the given script for each input line, -i.bak is like seds -i.

Related question: Why is there no generator that accepts the target string as input and provides the regex that will find it?

Well. Usually regexes are used with patterns meant to match multiple strings, and there it might be hard for a program to know what parts can be varying. Though if you're always looking for a fixed string, it would be somewhat simple to just escape the special characters. But then you wouldn't actually need a regex engine in the first place. It's just that they're rather ubiquitous in the common Unix tools.

You mentioned in the comments that:

Come to think of it, if a line matches this string, that is all I need to know to replace it: $GLOBALS['timechecks']=addTimeCheck_sparky

Something like

sed -- -e 's/^.*GLOBALS..timechecks..=addTimeCheck_sparky.*$/hello/'

could be used to match against that and replace the whole line. Granted, that would also match #GLOBALS_atimecheckses=addTimeCheck_sparky and related variants, since I cheated and just replaced all the special characters with .. But you get the idea.

Also, you can always take a backup copy if the original file first, then run diff original.txt processed.txt to review any changes.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3